package com.xy.modulemain

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.xy.baselibrary.base.BaseFragment
import com.xy.modulemain.api.ApiService
import com.xy.modulemain.api.MainNetWorkApi
import com.xy.modulemain.bean.User
import com.xy.network.bean.ResponseBean
import com.xy.network.observer.BaseObserver
import com.xy.network.observer.OneBaseObserver
import kotlinx.android.synthetic.main.main_module_fragment.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * @ClassName MainModuleFragment
 * @Author xy
 * @Date 2021/8/19 16:35
 * @Description TODO
 */
class MainModuleFragment:BaseFragment(),View.OnClickListener {
    lateinit var service: ApiService
    override fun setLayout(): Int {
        return R.layout.main_module_fragment
    }

    //加载数据
    override fun initData() {
        jump.setOnClickListener(this)
        test.setOnClickListener(this)
        net1.setOnClickListener(this)
        net2.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.test->{
                PermissionUtils.permission(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.INTERNET
                ).callback(object : PermissionUtils.SimpleCallback {
                    override fun onGranted() {
                        var intent = Intent(Intent.ACTION_PICK)
                        intent.setType("image/*")
                        startActivityForResult(intent, 200)
                    }

                    override fun onDenied() {
                        ToastUtils.showShort("请打开相关权限")
                    }

                }).request()
            }
            R.id.net1 -> {
                service = MainNetWorkApi.getService(ApiService::class.java)!!
                service?.login1("1", "1")?.enqueue(object : Callback<ResponseBean<User>> {
                    override fun onResponse(
                        call: Call<ResponseBean<User>>,
                        response: Response<ResponseBean<User>>
                    ) {
                        ToastUtils.showShort(response.body()?.data.toString())
                    }

                    override fun onFailure(call: Call<ResponseBean<User>>, t: Throwable) {
                        ToastUtils.showShort("请求失败")
                    }

                })
            }
            R.id.net2 -> {
                service = MainNetWorkApi.getService(ApiService::class.java)!!
                service?.login2("123", "123")
                    ?.compose(MainNetWorkApi.instance.applySchedulers(object :
                        OneBaseObserver<ResponseBean<User>>() {
                        override fun onSuccess(t: ResponseBean<User>) {
                            ToastUtils.showShort("请求成功${t.data.toString()}")
                        }

                        override fun onFailure(e: Throwable) {
                            ToastUtils.showShort(e.message)
                        }

                    }))
            }

            R.id.jump->{
                ActivityUtils.startActivity(MvvmActivity::class.java)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 200) {
                ToastUtils.showShort("回调成功")
            }
        }
    }
}